var platform_map = function(){ 
    var key = {
        gnss_centerid:NumberInt(this.gnss_centerid),
        owner_unit : this.owner_unit,
        platform_name:this.platform_name
    };
    var values = {
        analyse_date: this.analyse_date,
        enterprise_id:this.enterprise_id,
        vehicle_count:1,
        vehicle_access_count:this.has_net?1:0,
        vehicle_online_count:this.has_track?1:0,
        track_normal : this.track_normal,
        alarm_vehicle_count : this.alarm_count>0?1:0,
        location_exception_vehicle_count:this.location_exception_count>0?1:0,
        exception_vehicle_count:this.exception_track_count>0?1:0,
        alarm_count : this.alarm_count,
        alarm_processed : this.alarm_processed,
        overspeed_count : this.overspeed_count,
        overspeed_processed : this.overspeed_processed,
        roadoffset_count : this.roadoffset_count,
        roadoffset_processed : this.roadoffset_processed,
        tired_count : this.tired_count,
        tired_processed : this.tired_processed,
        tired_duration : this.tired_duration,
        prohibit_driving_count : this.prohibit_driving_count,
        prohibit_driving_processed : this.prohibit_driving_processed,
        nightly_3rd_count  : this.nightly_3rd_count,
        nightly_3rd_processed  : this.nightly_3rd_processed,
        over_load_count : this.over_load_count,
        over_load_processed : this.over_load_processed,
        overspeed_100_count : this.overspeed_100_count,
        speed_160_count  : this.speed_160_count,
        track_complete_rate : this.track_complete_rate,
        exception_track_count : this.exception_track_count,
        gpstime_exception_count : this.gpstime_exception_count,
        location_exception_count : this.location_exception_count,
        speed_exception_count : this.speed_exception_count,
        head_exception_count : this.head_exception_count,
        trade_kind_id:this.trade_kind_id,
        has_net : this.has_net,
        has_track : this.has_track,
        has_alarm : this.alarm_count>0,
        alarm_count_list : this.alarm_count,
        alarm_processed_list : this.alarm_processed,
        overspeed_count_list : this.overspeed_count,
        overspeed_processed_list : this.overspeed_processed,
        tired_count_list : this.tired_count,
        tired_processed_list : this.tired_processed,
        count:1
    };
    emit(key, values);
}; 
var platform_reduce = function(key, values){ 
    const reduce_result = {
        analyse_date: null,  
        enterprise_id:[],
        check_count: 0,                         
        reply_count: 0,                         
        reply_correct_count: 0,                 
        reply_rate: 0,                          
        vehicle_count: 0,                       
        vehicle_access_count: 0,                
        vehicle_online_count: 0,                
        track_normal: 0,                        
        alarm_vehicle_count : 0, 
        location_exception_vehicle_count : 0,
        exception_vehicle_count:0,
        alarm_count: 0,                         
        alarm_processed : 0,                    
        overspeed_count : 0,                    
        overspeed_processed : 0,                
        roadoffset_count: 0,                    
        roadoffset_processed: 0,                
        tired_count: 0,                         
        tired_processed : 0,                    
        tired_duration  : 0,                    
        prohibit_driving_count  : 0,            
        prohibit_driving_processed : 0,        
        nightly_3rd_count   : 0,                
        nightly_3rd_processed : 0,              
        over_load_count : 0,                    
        over_load_processed : 0,                
        overspeed_100_count : 0,  
        speed_160_count  : 0,
        track_complete_rate : 0,                
        exception_track_count   : 0,            
        gpstime_exception_count : 0,            
        location_exception_count: 0,            
        speed_exception_count   : 0,            
        head_exception_count: 0   ,
        trade_kind_id:[],
        has_net:[],
        has_track:[],
        has_alarm:[],
        alarm_count_list : [],
        alarm_processed_list : [],
        overspeed_count_list : [],
        overspeed_processed_list : [],
        tired_count_list : [],
        tired_processed_list : [],
        count : 0       
    };

    values.forEach(
        function(val){
            reduce_result.enterprise_id=reduce_result.enterprise_id.concat(val.enterprise_id);
            reduce_result.analyse_date =val.analyse_date ;
            reduce_result.vehicle_count +=val.vehicle_count ;
            reduce_result.vehicle_access_count += val.vehicle_access_count ;        
            reduce_result.vehicle_online_count += val.vehicle_online_count ;        
            reduce_result.track_normal += val.track_normal ;                
            reduce_result.alarm_vehicle_count += val.alarm_vehicle_count ; 
            reduce_result.location_exception_vehicle_count  += val.location_exception_vehicle_count ;   
            reduce_result.exception_vehicle_count   += val.exception_vehicle_count ;
            reduce_result.alarm_count += val.alarm_count ;                  
            reduce_result.alarm_processed += val.alarm_processed ;          
            reduce_result.overspeed_count += val.overspeed_count ;          
            reduce_result.overspeed_processed += val.overspeed_processed ;      
            reduce_result.roadoffset_count += val.roadoffset_count ;        
            reduce_result.roadoffset_processed += val.roadoffset_processed ;        
            reduce_result.tired_count += val.tired_count;                   
            reduce_result.tired_processed += val.tired_processed ;          
            reduce_result.tired_duration += val.tired_duration ;            
            reduce_result.prohibit_driving_count += val.prohibit_driving_count ;    
            reduce_result.prohibit_driving_processed += val.prohibit_driving_processed ;
            reduce_result.nightly_3rd_count += val.nightly_3rd_count ;      
            reduce_result.nightly_3rd_processed += val.nightly_3rd_processed ;      
            reduce_result.over_load_count += val.over_load_count ;      
            reduce_result.over_load_processed += val.over_load_processed ;      
            reduce_result.overspeed_100_count += val.overspeed_100_count ;
            reduce_result.speed_160_count  += val.speed_160_count;
            reduce_result.track_complete_rate += parseFloat(val.track_complete_rate) ;      
            reduce_result.exception_track_count += val.exception_track_count ;  
            reduce_result.gpstime_exception_count += val.gpstime_exception_count ;  
            reduce_result.location_exception_count += val.location_exception_count ;    
            reduce_result.speed_exception_count += val.speed_exception_count ;  
            reduce_result.head_exception_count += val.head_exception_count ;  
            reduce_result.trade_kind_id=reduce_result.trade_kind_id.concat(val.trade_kind_id);
            reduce_result.has_net=reduce_result.has_net.concat(val.has_net);
            reduce_result.has_track=reduce_result.has_track.concat(val.has_track);
            reduce_result.has_alarm=reduce_result.has_alarm.concat(val.has_alarm);
            reduce_result.alarm_count_list=reduce_result.alarm_count_list.concat(val.alarm_count_list);
            reduce_result.alarm_processed_list=reduce_result.alarm_processed_list.concat(val.alarm_processed_list);
            reduce_result.overspeed_count_list=reduce_result.overspeed_count_list.concat(val.overspeed_count_list);
            reduce_result.overspeed_processed_list=reduce_result.overspeed_processed_list.concat(val.overspeed_processed_list);
            reduce_result.tired_count_list=reduce_result.tired_count_list.concat(val.tired_count_list);
            reduce_result.tired_processed_list=reduce_result.tired_processed_list.concat(val.tired_processed_list);
            reduce_result.count += val.count ;          
        }
    ); 

    return reduce_result; 
}; 

var platform_finalize= function(key, values){
    if(values.track_normal>0){
        values.data_correct_rate = ((values.track_normal/(values.track_normal+values.exception_track_count))* 100).toFixed(2);
        values.data_fault_rate = (100-values.data_correct_rate);
    }else{
        values.data_correct_rate=0.0;
        values.data_fault_rate=0.0;
    }
    if(values.vehicle_online_count>0){
        values.location_exception_rate=parseFloat(((values.location_exception_vehicle_count/values.vehicle_online_count) * 100).toFixed(2));
        values.track_complete_rate=parseFloat(((values.track_complete_rate/values.vehicle_online_count)).toFixed(2));
    }else{
        values.location_exception_rate=0.0;
        values.track_complete_rate=0.0;
    }
    if(values.count>0){
        values.vehicle_access_rate=parseFloat(((values.vehicle_access_count/values.count)* 100).toFixed(2));
    }else{
        values.vehicle_access_rate=0.0;
    }
    if(values.vehicle_access_count>0){
        values.vehicle_online_rate=parseFloat(((values.vehicle_online_count/values.vehicle_access_count)* 100).toFixed(2));
    }else{
        values.vehicle_online_rate=0.0;
    }
    if(values.vehicle_online_count>0){
        values.vehicle_alarm_rate=parseFloat(((values.alarm_vehicle_count/values.vehicle_online_count)* 100).toFixed(2));
    }else{
        values.vehicle_alarm_rate=0.0;
    }
    var trade_obj={
        1:{trade_name:'班线客运',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        2:{trade_name:'旅游客运',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        3:{trade_name:'危品运输',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        4:{trade_name:'公交车',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        5:{trade_name:'出租车',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0},
        6:{trade_name:'其他',vehicle_count:0,vehicle_access_count:0,vehicle_online_count:0,alarm_vehicle_count:0,vehicle_access_rate:0.0,vehicle_online_rate:0.0,alarm_vehicle_rate:0.0,alarm_count:0,alarm_processed:0,overspeed_count:0,overspeed_processed:0,tired_count:0,tired_processed:0,avg_overspeed_count:0,avg_tired_count:0,alarm_processed_rate:0.0}
    };  
	
	for (var key in trade_obj) {
		trade_obj[key]['vehicle_count']=NumberInt(0);
		trade_obj[key]['vehicle_access_count']=NumberInt(0);
		trade_obj[key]['vehicle_online_count']=NumberInt(0);
		trade_obj[key]['alarm_vehicle_count']=NumberInt(0);
		trade_obj[key]['alarm_count']=NumberInt(0);
		trade_obj[key]['alarm_processed']=NumberInt(0);
		trade_obj[key]['overspeed_count']=NumberInt(0);
		trade_obj[key]['overspeed_processed']=NumberInt(0);
		trade_obj[key]['tired_count']=NumberInt(0);
		trade_obj[key]['tired_processed']=NumberInt(0);
	}
	
    for (var i in values.trade_kind_id) {
        if(trade_obj[values.trade_kind_id[i]]){
            trade_obj[values.trade_kind_id[i]].vehicle_count+=1;
            trade_obj[values.trade_kind_id[i]].vehicle_count=NumberInt(trade_obj[values.trade_kind_id[i]].vehicle_count);
            if(values.has_net[i]){
                trade_obj[values.trade_kind_id[i]].vehicle_access_count+=1;
                trade_obj[values.trade_kind_id[i]].vehicle_access_count=NumberInt(trade_obj[values.trade_kind_id[i]].vehicle_access_count);
            }
            if(values.has_track[i]){
                trade_obj[values.trade_kind_id[i]].vehicle_online_count+=1;
                trade_obj[values.trade_kind_id[i]].vehicle_online_count=NumberInt(trade_obj[values.trade_kind_id[i]].vehicle_online_count);
            }
            if(values.has_alarm[i]){
                trade_obj[values.trade_kind_id[i]].alarm_vehicle_count+=1;
                trade_obj[values.trade_kind_id[i]].alarm_vehicle_count=NumberInt(trade_obj[values.trade_kind_id[i]].alarm_vehicle_count);
            }
            trade_obj[values.trade_kind_id[i]].alarm_count+=values.alarm_count_list[i];
            trade_obj[values.trade_kind_id[i]].alarm_count=NumberInt(trade_obj[values.trade_kind_id[i]].alarm_count);
            trade_obj[values.trade_kind_id[i]].alarm_processed+=values.alarm_processed_list[i];
            trade_obj[values.trade_kind_id[i]].alarm_processed=NumberInt(trade_obj[values.trade_kind_id[i]].alarm_processed);
            trade_obj[values.trade_kind_id[i]].overspeed_count+=values.overspeed_count_list[i];
            trade_obj[values.trade_kind_id[i]].overspeed_count=NumberInt(trade_obj[values.trade_kind_id[i]].overspeed_count);
            trade_obj[values.trade_kind_id[i]].overspeed_processed+=values.overspeed_processed_list[i];
            trade_obj[values.trade_kind_id[i]].overspeed_processed=NumberInt(trade_obj[values.trade_kind_id[i]].overspeed_processed);
            trade_obj[values.trade_kind_id[i]].tired_count+=values.tired_count_list[i];
            trade_obj[values.trade_kind_id[i]].tired_count=NumberInt(trade_obj[values.trade_kind_id[i]].tired_count);
            trade_obj[values.trade_kind_id[i]].tired_processed+=values.tired_processed_list[i];
            trade_obj[values.trade_kind_id[i]].tired_processed=NumberInt(trade_obj[values.trade_kind_id[i]].tired_processed);
            
            if(trade_obj[values.trade_kind_id[i]].vehicle_count>0){
                trade_obj[values.trade_kind_id[i]].vehicle_access_rate=parseFloat((trade_obj[values.trade_kind_id[i]].vehicle_access_count/trade_obj[values.trade_kind_id[i]].vehicle_count*100).toFixed(2));
            }
            if(trade_obj[values.trade_kind_id[i]].vehicle_access_count>0){
                trade_obj[values.trade_kind_id[i]].vehicle_online_rate=parseFloat((trade_obj[values.trade_kind_id[i]].vehicle_online_count/trade_obj[values.trade_kind_id[i]].vehicle_access_count*100).toFixed(2));
            }
            if(trade_obj[values.trade_kind_id[i]].vehicle_online_count>0){
                trade_obj[values.trade_kind_id[i]].alarm_vehicle_rate=parseFloat((trade_obj[values.trade_kind_id[i]].alarm_vehicle_count/trade_obj[values.trade_kind_id[i]].vehicle_online_count*100).toFixed(2));
                trade_obj[values.trade_kind_id[i]].avg_overspeed_count=parseFloat((trade_obj[values.trade_kind_id[i]].overspeed_count/trade_obj[values.trade_kind_id[i]].vehicle_online_count).toFixed(2));
                trade_obj[values.trade_kind_id[i]].avg_tired_count=parseFloat((trade_obj[values.trade_kind_id[i]].tired_count/trade_obj[values.trade_kind_id[i]].vehicle_online_count).toFixed(2));
            }
            if(trade_obj[values.trade_kind_id[i]].alarm_count>0){
                trade_obj[values.trade_kind_id[i]].alarm_processed_rate=parseFloat((trade_obj[values.trade_kind_id[i]].alarm_processed/trade_obj[values.trade_kind_id[i]].alarm_count).toFixed(2));
            }   
        }
    }
    if(values.enterprise_id.length>0){
        var res = [];
        var json = {};
        for(var i = 0; i < values.enterprise_id.length; i++){
            if(!json[values.enterprise_id[i]]){
                res.push(values.enterprise_id[i]);
                json[values.enterprise_id[i]] = 1;
            }
        }
        values.enterprise_count=res.length;
    }else{
        values.enterprise_count=0;
    }
    values.trade_obj=trade_obj;
    delete value.enterprise_id;
	delete value.trade_kind_id;
	delete value.has_net;
	delete value.has_track;
	delete value.has_alarm;
	delete value.alarm_count_list;
	delete value.alarm_processed_list;
	delete value.overspeed_count_list;
	delete value.overspeed_processed_list;
	delete value.tired_count_list;
	delete value.tired_processed_list;
	delete value.count;
    return values;
}
    
db.runCommand({
    mapReduce: 'vehicle_swap_week',
    map: platform_map,
    reduce: platform_reduce,
    finalize: platform_finalize,
    out: {
        replace: 'platform_swap_temp_1108'
    },
    sort: {
        _id: 1
    },query: {
        analyse_date : ISODate("2017-11-08T16:00:00Z")
    }
});
/*没有的列
disconnect_count  从平台天表来
online_time  连通时间
online_rate 连通率
total_time  算连通率的
score 得分*/